#!/bin/bash
apt-get purge needrestart -y
apt update
apt-get install -y gcc g++ make cmake-curses-gui pkg-config


rm -rf $MY_FFMPEG
mkdir -p $MY_FFMPEG
pwds=$(pwd)

#环境变量 "/var/opt/myffmpeg/lib/"
#export PKG_CONFIG_PATH="${MY_FFMPEG}/lib/pkgconfig:${MY_FFMPEG}/lib64/pkgconfig"
cat > /etc/profile.d/myffmpeg.sh << EOF
#!/bin/bash
export MY_FFMPEG=/var/opt/myffmpeg-4-4-4
export PKG_CONFIG_PATH="\${MY_FFMPEG}/lib/pkgconfig:\${MY_FFMPEG}/lib64/pkgconfig"
export PATH=\$MY_FFMPEG/bin:\$PATH

EOF

source /etc/profile

rm -rf $MY_FFMPEG
mkdir -p $MY_FFMPEG
echo "/var/opt/myffmpeg-4-4-4/lib" > /etc/ld.so.conf.d/ffmpeg.conf
echo "/var/opt/myffmpeg-4-4-4/lib64" >> /etc/ld.so.conf.d/ffmpeg.conf
ldconfig
# 编译安装Nettle-3.6 gnutls-3.7.0 依赖
#wget https://ftp.gnu.org/gnu/nettle/nettle-3.6.tar.gz
cd $pwds
rm -rvf nettle-3.6
tar -xvf nettle-3.6.tar.gz
cd nettle-3.6
# nettle 依赖--enable-shared
apt-get -y install m4
./configure --prefix=$MY_FFMPEG --disable-shared --enable-mini-gmp --disable-enable 
make -j4
make install
cd $pwds
rm -rvf nettle-3.6

# gnutls 工具
# https://www.cmdschool.org/archives/12469
#wget https://www.gnupg.org/ftp/gcrypt/gnutls/v3.7/gnutls-3.7.0.tar.xz
cd $pwss
rm -rf gnutls-3.7.0
tar -xvf gnutls-3.7.0.tar.xz
cd gnutls-3.7.0

#  gmp was not found
apt-get install -y libgmp-dev
# Libtasn1 4.9 was not found. To use the included one, use --with-included-libtasn1
apt-get install -y libtasn1-6-dev
# Libunistring was not found. To use the included one, use --with-included-unistring
apt install libunistring-dev
#  libunbound was not found. Libdane will not be built
apt-get install -y libunbound-dev
# p11-kit >= 0.23.1 was not found. To disable PKCS #11 support
apt-get install -y libp11-kit-dev
./configure \
--prefix=$MY_FFMPEG \
--enable-shared \
--enable-mini-gmp \
--disable-static \
--disable-guile \
--without-p11-kit

make -j4
make install

cd $pwss
rm -rf gnutls-3.7.0

# ERROR: libfdk_aac not found

rm -rf fdk-aac-2.0.3
tar -xvf fdk-aac-2.0.3.tar.gz
cd fdk-aac-2.0.3
#   --enable-shared -disable-static
./configure --prefix=$MY_FFMPEG  --enable-static --disable-shared
make -j4
make install
cd $pwds
rm -rf fdk-aac-2.0.3
# ERROR: libmp3lame >= 3.98.3 not found
# wget https://zenlayer.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
rm -rf lame-3.100
tar -xvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix=$MY_FFMPEG  --enable-static --disable-shared
make -j4
make install
cd $pwds
rm -rf lame-3.100

# ERROR: speex not found using pkg-config
# wget https://ftp.osuosl.org/pub/xiph/releases/speex/speex-1.2.1.tar.gz
rm -rf speex-1.2.1
tar -xvf speex-1.2.1.tar.gz
cd speex-1.2.1
./configure --prefix=$MY_FFMPEG  --enable-shared --disable-static
make -j4
make install
cd $pwds
rm -rf speex-1.2.1

# ERROR: libx264 not found
 rm -rf x264
tar -xvf x254.tar.gz
#git clone https://gitee.com/xiao2327/x264.git
 cd x264/
./configure --prefix=$MY_FFMPEG  --enable-shared --disable-asm --disable-static
make -j4
make install
cd $pwds
 rm -rf x264
#ERROR: x265 not found using pkg-config
 rm -rf x265
tar -xvf x255.tar.gz 
#git clone https://gitee.com/umu618/x265.git

cd $pwds/x265/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$MY_FFMPEG ../../source
make -j4
make install
cd $pwds
 rm -rf x265
# ffmpeg 编译安装
cd $pwds
rm -rf ffmpeg-4.4.4
tar -xvf ffmpeg-4.4.4.tar.gz
cd ffmpeg-4.4.4
#nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
apt-get install -y  yasm nasm
# ERROR: gnutls not found using pkg-config
apt install -y libgnutls28-dev

./configure \
    --prefix=$MY_FFMPEG \
	--enable-shared \
	--disable-static \
 	--enable-gpl \
 	--enable-nonfree \
 	--enable-gnutls \
 	--enable-libmp3lame \
    --enable-libfdk-aac \
    --enable-libx264 \
    --enable-libx265 \
    --enable-filter=delogo \
    --enable-debug \
    --enable-optimizations \
    --enable-libspeex \
     --enable-pthreads \
     --enable-version3 \
     --enable-hardcoded-tables \
    --arch=x86_64


make -j4
make install
cd $pwds

rm -rf ffmpeg-4.3.6




